#include"string.h"
namespace ForcibleBugMaker
{
	const size_t string::npos = -1;


	string::string(const char* str)
		:_size(strlen(str))
	{
		_capacity = _size + 1;
		_str = new char[_capacity + 1];
		strcpy(_str, str);
	}

	string::string(const string& s)
	{
		_str = new char[s._capacity + 1];
		strcpy(_str, s._str);
		_size = s._size;
		_capacity = s._capacity;
	}
	
	string& string::operator=(string tmp)
	{
		swap(tmp);
		return *this;
	}

	string::~string()
	{
		delete[] _str;
		_str = nullptr;
		_capacity = 0;
		_size = 0;
	}

	string::iterator string::begin()
	{
		return _str;
	}

	string::iterator string::end()
	{
		return _str + _size;
	}

	string::const_iterator string::begin() const
	{
		return _str;
	}

	string::const_iterator string::end() const
	{
		return _str + _size;
	}
	
	
	const char* string::c_str() const
	{
		return _str;
	}

	size_t string::size() const
	{
		return _size;
	}

	size_t string::capaity() const
	{
		return _capacity;
	}

	char& string::operator[](size_t pos)
	{
		assert(pos < _size);
		return _str[pos];
	}

	const char& string::operator[](size_t pos) const
	{
		assert(pos < _size);
		return _str[pos];
	}


	void string::reserve(size_t n)
	{
		if (_capacity < n) {
			char* tmp = new char[n + 1];
			strcpy(tmp, _str);
			delete[] _str;
			_str = tmp;
			_capacity = n;
		}
	}

	void string::push_back(char ch)
	{
		if (_size >= _capacity) {
			reserve(_capacity == 0 ? 4 : _capacity * 2);
		}
		_str[_size] = ch;
		++_size;
		_str[_size] = '\0';
	}

	void string::append(const char* str)
	{
		size_t len = strlen(str);
		if (_capacity < _size + len) {
			reserve(_size + len);
		}
		strcpy(_str + _size, str);
		_size += len;
	}

	string& string::operator+=(char ch)
	{
		push_back(ch);
		return *this;
	}

	string& string::operator+=(const char* str)
	{
		append(str);
		return *this;
	}


	void string::insert(size_t pos, char ch)
	{
		assert(pos <= _size);
		if (_size >= _capacity) {
			reserve(_capacity == 0 ? 4 : _capacity * 2);
		}
		size_t end = _size + 1;
		while (end > pos) {
			_str[end] = _str[end - 1];
			--end;
		}
		_str[pos] = ch;
		++_size;
	}

	void string::insert(size_t pos, const char* str)
	{
		assert(pos <= _size);
		size_t len = strlen(str);
		if (len == 0)return;
		if (_size + len > _capacity) {
			reserve(_size + len);
		}
		size_t end = _size + len;
		while (end > pos + len - 1) {
			_str[end] = _str[end - len];
			--end;
		}
		memcpy(_str + pos, str, len);
		_size += len;
	}
	
	void string::erase(size_t pos, size_t len) 
	{
		assert(pos < _size);
		if (len >= _size - pos) {
			_str[pos] = '\0';
			_size = pos;
		}
		else {
			strcpy(_str + pos, _str + pos + len);
			_size -= len;
		}
	}

	size_t string::find(char ch, size_t pos)
	{
		for (size_t i = pos; i < _size; i++) {
			if (_str[i] == ch)
				return i;
		}
		return npos;
	}

	size_t string::find(const char* sub, size_t pos)
	{
		const char* ptr = strstr(_str, sub);
		return ptr - _str;
	}

	bool string::operator>(const string& str) const
	{
		return strcmp(_str, str._str) > 0;
	}
	bool string::operator==(const string& str) const
	{
		return strcmp(_str, str._str) == 0;
	}
	bool string::operator>=(const string& str) const
	{
		return *this > str || *this == str;
	}
	bool string::operator<(const string& str) const
	{
		return !(*this >= str);
	}
	bool string::operator<=(const string& str) const
	{
		return !(*this > str);
	}
	bool string::operator!=(const string& str) const
	{
		return !(*this == str);
	}

	string string::substr(size_t pos, size_t len)
	{
		if (pos + len >= _size) {
			string sub(_str + pos);
			return sub;
		}
		else {
			string sub;
			sub.reserve(len);
			for (size_t i = 0; i < len; i++) {
				sub += _str[pos + i];
			}
			return sub;
		}
	}

	void string::swap(string& str)
	{
		std::swap(_str, str._str);
		std::swap(_size, str._size);
		std::swap(_capacity, str._capacity);
	}
	
	void string::clear()
	{
		_str[0] = '\0';
		_size = 0;
	}

	void swap(string& s1, string s2)
	{
		std::swap(s1._str, s2._str);
		std::swap(s1._size, s2._size);
		std::swap(s1._capacity, s2._capacity);
	}

	std::ostream& operator<<(std::ostream& os, const string& str)
	{
		for (int i = 0; i < str.size(); i++)
			os << str[i];
		return os;
	}

	std::istream& operator>>(std::istream& is, string& str)
	{
		str.clear();
		char ch = is.get();
		while (ch != ' ' && ch != '\n') {
			str += ch;
			ch = is.get();
		}
		return is;
	}

}
